package com.feshfans.xmpp.client;

import com.feshfans.xmpp.XmppConnectionManager;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.tcp.XMPPTCPConnection;
import org.jivesoftware.smackx.vcardtemp.VCardManager;
import org.jivesoftware.smackx.vcardtemp.packet.VCard;
import org.jxmpp.jid.BareJid;
import org.jxmpp.jid.EntityBareJid;
import org.jxmpp.jid.EntityFullJid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class AccountClient extends AbstractClient {

    private static final Logger Log = LoggerFactory.getLogger(AccountClient.class);

    private static final AccountClient ACCOUNT_CLIENT = new AccountClient(XmppConnectionManager.acquire());

    public static AccountClient getInstance(){
        return ACCOUNT_CLIENT;
    }
    AccountClient(XMPPTCPConnection connection) {
        super(connection);
    }

    public boolean login(String username,String password){
        if(connection.isAuthenticated()){
            return true;
        }
        try {
            connection.login(username,password);
            return true;
        } catch (Exception e) {
            Log.error("用户 {} 登录出错", username, e);
            return false;
        }
    }

    public boolean register(String username, String password){
        return false;
    }

    /**
     * 获取当前用户的 VCard
     * @return VCard
     * @throws XMPPException.XMPPErrorException
     * @throws SmackException.NotConnectedException
     * @throws InterruptedException
     * @throws SmackException.NoResponseException
     */
    public VCard getSelfVCard() throws XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException, SmackException.NoResponseException {
        VCardManager vCardManager = VCardManager.getInstanceFor(connection);
        return vCardManager.loadVCard();
    }

    public VCard getVCard(EntityBareJid jid) throws XMPPException.XMPPErrorException, SmackException.NotConnectedException, InterruptedException, SmackException.NoResponseException {
        VCardManager vCardManager = VCardManager.getInstanceFor(connection);

        return vCardManager.loadVCard(jid);
    }
}
